package com.xzw.test.springcloud.contorller;

import com.xzw.test.springcloud.model.CommonResult;
import com.xzw.test.springcloud.model.Payment;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@Slf4j
public class OrderController {


    //public static final String PAYMENT_URL = "http://localhost:8001";

    // 注册中心服务名称
    public static final String PAYMENT_URL = "http://CLOUD-PAYMENT-SERVICE";
    //public static final String PAYMENT_URL="http://cloud-payment-service";


    @Autowired
    private RestTemplate restTemplate;

    @PostMapping("/consumer/payment/insert")
    public CommonResult<Payment> insert(Payment payment){
        log.info("====== 请求插入：" + payment);
        return restTemplate.postForObject(PAYMENT_URL + "/payment/insert", payment, CommonResult.class);
    }
    
    @GetMapping("/consumer/payment/get/{id}")
    public CommonResult<Payment> getPayment(@PathVariable("id") Long id){
        log.info("获取支付请求id:{}",id);
        //调用提供者，返回参数对象要序列号，否则会出现返回异常。
        //直接负载到服务提供者地址
        return restTemplate.getForObject(PAYMENT_URL + "/payment/get/" + id, CommonResult.class);
    }
}
